<?php
	//Connect to database
	require_once $_SERVER['DOCUMENT_ROOT'] . '/deviantpic/inc/database.php';

	//Skip if no data
	if (!isset($_POST['id'])) {	echo ""; die();	}

	//Prepare variable
	$tag_ids		= array();
	$chat_ids		= array();
	$follow_ids		= array();
	$chatter_ids	= array();
	$notifications	= array();

	//Check follower
	$result = mysql_query("SELECT * FROM followers WHERE user_id=" . $_POST['id'] . " AND notified=0") or die(mysql_error());
	if (mysql_num_rows($result) > 0) {
		//While there's a row
		$row = mysql_fetch_array($result);
		while ($row) {
			//Get follower name
			$user_result = mysql_query("SELECT username FROM users WHERE id=" . $row['follower_id']);
			if (mysql_num_rows($user_result) > 0) {
				//Get row
				$user_row = mysql_fetch_array($user_result);

				//Save
				$notif				= array();
				$follow_ids[]		= $row['id'];
				$notif['type']		= "follow";
				$notif['user_id']	= $row['user_id'];
				$notif['username']	= $user_row['username'];
				$notifications[]	= $notif;
			}

			//Next
			$row = mysql_fetch_array($result);
		}
	}

	//Check chat
	$result = mysql_query("SELECT * FROM chats WHERE receiver_id=" . $_POST['id'] . " AND notified=0 AND been_read=0") or die(mysql_error());
	if (mysql_num_rows($result) > 0) {
		//While there's a row
		$row = mysql_fetch_array($result);
		while ($row) {
			//Check is the sender already saved
			if (!in_array($row['sender_id'], $chatter_ids)) {
				
				//Get chatter name
				$user_result = mysql_query("SELECT username FROM users WHERE id=" . $row['sender_id']);
				if (mysql_num_rows($user_result) > 0) {
					//Get row
					$user_row = mysql_fetch_array($user_result);

					//Save
					$notif				= array();
					$chatter_ids[]		= $row['sender_id'];
					$notif['type']		= "chat";
					$notif['chat']		= $row['message'];
					$notif['user_id']	= $row['sender_id'];
					$notif['username']	= $user_row['username'];
					$notifications[]	= $notif;
				}
			}

			//Next
			$row = mysql_fetch_array($result);
		}
	}

	//Check tag
	$result = mysql_query("SELECT * FROM tags WHERE user_id=" . $_POST['id'] . " AND is_tagged=1 AND notified=0 AND is_owner=0") or die(mysql_error());
	if (mysql_num_rows($result) > 0) {
		//While there's a row
		$row = mysql_fetch_array($result);
		while ($row) {
			//Save
			$notif					= array();
			$tag_ids[]				= $row['id'];
			$notif['type']			= "tag";
			$notif['picture_id']	= $row['photo_id'];
			$notifications[]		= $notif;

			//Next
			$row = mysql_fetch_array($result);
		}
	}

	//Update notification
	foreach ($tag_ids as $tag_id)			mysql_query ("UPDATE tags SET notified='1' WHERE id=" . $tag_id);
	foreach ($follow_ids as $follow_id)		mysql_query ("UPDATE followers SET notified='1' WHERE id=" . $follow_id);
	foreach ($chatter_ids as $chatter_id)	mysql_query ("UPDATE chats SET notified='1' WHERE sender_id=" . $chatter_id . " AND receiver_id=" . $_POST['id']);

	//For each notifications
	foreach ($notifications as $notification) {
?>
	<li class="notification">
		<?php
			if ($notification['type'] == "follow") {
				echo '<a href="/deviantpic/user/profile/index.php?id=' . $notification['user_id'] . '">' . $notification['username'] . '</a> is following you!';
			} else if ($notification['type'] == "chat") {
				echo '<a href="/deviantpic/user/profile/index.php?id=' . $notification['user_id'] . '">' . $notification['username'] . "</a> ";
				echo 'said "' . $notification['chat'] . '" in ';
				echo '<a href="/deviantpic/user/profile/chat.php?id=' . $notification['user_id'] . '">chat</a>';
			} else if ($notification['type'] == "tag") {
				echo 'You have been tagged on a <a href="/deviantpic/picture/view.php?id=' . $notification['picture_id'] . '">picture</a>';
			}
		?>
	</li>
<?php
	}
?>
